<?php
session_start();

//echo "<pre>";
//print_r($_SESSION);
//print_r($data);exit;
require 'include/config.inc.php';
include FB_ABSPATH."/db/opendb.php";

if(isset($_REQUEST['fb_signed_id'])) {
	$fb_signed_id = $_REQUEST['fb_signed_id'];
}
else {
	//Check if user has fb user id on our main application
	$signed_request = $_REQUEST["signed_request"];

	list($encoded_sig, $payload) = explode('.', $signed_request, 2);

	$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

	//echo "<pre>";print_r($data);exit;
	$fb_signed_id = $data['user_id'];
}

$sql1 = "SELECT * FROM facebook_user_map WHERE fb_signed_id='$fb_signed_id'";
$rs = mysql_query($sql1);
if(mysql_num_rows($rs) > 0) {

	$row = mysql_fetch_assoc($rs);
	$user_id = $row['user_id'];

	include FB_ABSPATH."/facebook/classes/Paginator.class.php";
	$sql = "select p.* from PROPERTIES p inner join Prop_Owner_Con c on p.OID = c.Prop_OID where c.Owner = '".$user_id."' and LISTSTATUS = 'Active'";
	$rs = mysql_query($sql);
	$total_recs = mysql_num_rows($rs);
	$limit = 5;
	$from = isset($_REQUEST['page_'])?(int)$_REQUEST['page_']:1;

	$offset = ($from-1)*$limit;

	$mquery = "select p.* from PROPERTIES p inner join Prop_Owner_Con c on p.OID = c.Prop_OID where c.Owner = '".$user_id."' and LISTSTATUS = 'Active' LIMIT $offset, $limit";
	$mresult = mysql_query($mquery) or die ("SQL Error".mysql_error());

	$params = $_REQUEST;
	$pagination = Paging::getPagingFBML($total_recs, $limit, $params, CANVAS_URL);
	if(!isset($_REQUEST['fb_signed_id'])) {
?>
<link type="text/css" rel="stylesheet" href="<?php echo CANVAS_URL?>/style/style.css" />
<script>
<!--
function loadListing(id) {
	var ajax = new Ajax();
	ajax.responseType = Ajax.RAW;

	ajax.ondone = function(data) {
		//		alert(data)
		document.getElementById('main_container').setInnerXHTML(data);
	}
	ajax.post('http://realivent.info/facebook/tab.php?fb_signed_id=<?php echo $fb_signed_id?>&page_='+id);
}
//-->
</script>
<div class="main_container" id="main_container">
<?php
	}
?>
<div class="list_head" id="list_head">My Listings</div>
<?php echo $pagination;?>
<table width="100%"  border="0"><?php
if(mysql_num_rows($mresult))
{
	while ($row = mysql_fetch_assoc($mresult))
	{
		$query = "select * from Listing_Images where Listing_ID = '".$row['OID']."' and Main_Photo = 1";
		$result = mysql_query($query) or die ("SQL Error".mysql_error());
		$lrow = mysql_fetch_assoc($result);
		if($lrow['Path']!='')
		{
			$img_url = $lrow['Server_Path']."t_".$lrow['Path'];
		}
		else
		{
			$img_url = "http://www.realivent.com/listing_images/nia_thumb.gif";
		}
		$title = $row['STREETNUM']." ".$row['STREETNAME']." ".$row['STREETSUFFIX'].", ".$row['CITY']." ".$row['STATE'];
		$url = SITE_URL."/listing_details.php?listing_id=".$row['OID'];
		$description = $row['BEDS'] ." Beds | ".$row['BATHSTOTAL']." Baths | ".number_format($row['SQFTTOTAL'], 0)." SQFT | Covered Parking: ".$row['PARKINGCOVERED'];
		$price = number_format($row['LISTPRICE'], 0);
		?>
<TR><td width="120"><img style="border:1px solid black;" src="<?php echo $img_url?>" /></td><td><div class="list_title"><?php echo $title ?></div><div><?php echo $description?></div><div><b>Price:</b> $<?php echo number_format($row['LISTPRICE'], 0) ?></div><div><a href="<?php echo SITE_URL?>/listing_details.php?listing_id=<?php echo $row['OID']?>" target="_blank">View Listing:</a></div></td></TR><?php
	}
}
else {
	echo "<tr><td>No listings found</td></tr>";
}
?>
</table><?php
if(!isset($_REQUEST['fb_signed_id'])) {
?>
</div>
<?php
}
}
else {
	?>
	<a href="<?php echo CANVAS_URL?>assign_user.php?fb_user_id=<?php echo $fb_signed_id?>" target="_blank">click here to login</a>
	<?php
}
?>